<?php

require_once APPPATH.'controllers/gnc_mng'.EXT;

class Log_keluar_masuk extends Gnc_mng {
	protected $model_path = 'lkm/m_keluar_masuk';

	function resume() {

		//print_r_pre($data);
		$tanggal_log = $this->input->post('tanggal_log');

		//echo "dsddddddddd:".$tanggal_log;

		$data = array(
			'application' => get_application($this),
			'application_title' => $this->common->get_application_description(get_application($this)),
			'unit' => get_unit($this),
			'unit_title' => $this->common->get_unit_description(get_unit($this)),
			'unit_controller' => get_unit_controller($this),
			'page' => 'View Data',
			'navigation' => $this->load->view('lkm/nav/general', '', TRUE),
			'content' => $this->view_content_log_by_date($tanggal_log),
		);
		$this->load->view('layout/default', $data);		
		
	}
	
	protected function view_content_log_by_date($tanggal_log = NULL) {
		
		if (is_null_or_false($tanggal_log)) {
			$tanggal_log = date("Y-m-d");
		}
		
		//echo "dsddddddddd:".$tanggal_log;

		$data_log = $this->model->get_log_by_date($tanggal_log);
		//print_r_pre($data_log);
		$content_data = array(
			'controller' => get_unit_controller($this),
			'tanggal_log' => $tanggal_log,
			'data' => $data_log,	
		);
			
		$content = $this->load->view('lkm/viewer/log_by_date', $content_data, TRUE);
		
		return $content;	
	}

	public function process_import() {
		$import_data = NULL;
		parent::process_import_input($import_data);
		$this->process_import_process($import_data);
		parent::process_import_output($import_data);	
	}

	protected function validate_import_data($data) {
		if(($data[0]!="") 
				&& ($data[1]!="") 
					&& ($data[2]!="" 
						&& $this->model->is_student($data[0]) 
							&& !$this->model->is_data_exist($data[0],$data[1],$data[2]) )  )
			return true;
		else
			return false;
	}

	protected function process_import_process(&$import_data = NULL) {
		if (is_null($import_data)) $import_data = array();
		
		$derr = array();
		if (isset($import_data['import_file'])) {

			if (($handle = fopen($import_data['import_file'], "r")) !== FALSE) {

				$this->db->trans_begin();
				
				$error = false;
				$row = fgets($handle);
				if(count(explode("#",$row)) >1 ){
					$delimiter =  "#";
				}else if(count(explode(";",$row)) >1 ) {
					$delimiter =  ";";
				}else{
					$delimiter =  ",";
				}
				
				//print_r_pre($row);
				
				rewind ($handle);
				$fields = array("key_aka_student", "dt_log","key_lkm_status");
				while (($data = fgetcsv($handle,0,$delimiter)) !== FALSE) {
					//echo count($data)."<br>";
					//$metadata = $this->model->get_metadata_from_query();
					
					//print_r_pre($data);

					//exit;
					//$row++;
					
	
					if ($this->validate_import_data($data)) {
						$insert_data = array();
						foreach ($fields as $idx => $field) {
							if (!is_empty($idx) && !is_null_or_false($idx)) {
								$insert_data[$field] = $data[$idx];
							}
						}

						$this->common->who_am_i($insert_data, true, 'crea');
						$status = $this->model->add($insert_data);								
						
						if ($status === FALSE) {
							//echo "tets";
							//print_r_pre($data);
							$error = true;
							//exit;
							break;
						}
					} 
					
				}
				
				if (($error) || ($this->db->trans_status() === FALSE)) {
					$import_data['error'] = true;
					$this->db->trans_rollback();					
				} else {
					$import_data['error'] = false;
					$import_data['n_rows'] = $row-2;
					$this->db->trans_commit();
				}
				
				$this->db->trans_off();
				
				fclose($handle);
				//exit;
			}		
		}		
	}
}

/* EOF modules/apl/controllers/test_1.php */